const app = getApp()
const bdParse = require('../../bdParse/bdParse.js');
const util = require('../../util/util');
import { baseUrl } from '../../api/api'
import { requestApi } from '../../api/request'

Page({
    data: {
        contentauto:false,
        preloading:true,
        param:{
            action:'detail',
            id:64130
        },
        commentParam:{},
        isParamOk:false,
        detailsid:''
    },
    onInit(option){
        let param = this.data.param
        this.data.param.id = option.id
        this.setData({
            id:option.id
        })
        this.getDetails(param)
    },
    onLoad(){},
    // showMyFavoriteGuide: function () {
    //     swan.showFavoriteGuide({
    //         type: 'bar',
    //         content: '一键关注小程序',
    //         success: res => {
    //             console.log('关注成功：', res);
    //         },
    //         fail: err => {
    //             console.log('关注失败：', err);
    //         }
    //     })
    // },
    onShow: function() {
    },
    //查看全文
    tapclickall(){
        var that = this
        that.setData({ contentauto : true })
    },
    getDetails(param){
        requestApi(param).then(
            res => {
                // console.log(res)
                // console.log(baseUrl)
                let title = res.data.title
                let time = res.data.time
                let writer = res.data.writer ? res.data.writer : '武汉生活网'
                let content = util.baseDecode(res.data.content)
                let aboutlist = res.data.about
                let hotlist = res.data.hot
                let newslist = aboutlist.concat(hotlist)
                let site = {
                            img:res.data.image,
                            siteintro:res.data.description,
                            sitekey:res.data.keywords,
                            title,
                        }
                bdParse.bdParse('article', 'html', content, this, 5)
                this.setData({
                    preloading:false,
                    title,
                    time,
                    writer,
                    newslist
                })
                app.getTdk(site)
                this.getOpenid(res)
            }
        )
    },
    getOpenid(resdata) {
        swan.getLoginCode({
            success: res => {
                swan.request({
                    url: 'https://spapi.baidu.com/oauth/jscode2sessionkey',
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                        code: res.code,
                        client_id: baseUrl.appKey, // eslint-disable-line
                        sk: baseUrl.appSecret
                    },
                    success: res => {
                        var that = this
                        console.log(res)
                        // console.log(baseUrl.app.app_key)
                        // console.log("netData getOpenid res", res)
                        if (res.statusCode == 200) {
                            // 这里是使用获取到的用户openid
                            console.log(res)
                            that.setData({
                                commentParam: {
                                    openid: res.data.openid,
                                    snid: this.data.param.id,
                                    path: '/pages/details/details?id=' + this.data.param.id,
                                    title: resdata.data.title,
                                    images: resdata.data.image,
                                },
                                toolbarConfig: {
                                    share: {
                                        title: resdata.data.title,
                                    },
                                    moduleList: ['comment', 'like', 'favor', 'share'],
                                    placeholder: "回复评论"
                                },
                                isParamOk: true,
                            });
                            // console.log(resdata)
                            // console.log("commentParam2 ", that.data.commentParam)
                            // console.log("isParamOk ", that.data.isParamOk)
                        }
                    },
                    fail: function (err) {
                        console.log('getOpenid fail', err);
                    }
                });
            }, fail: function (err) {
                console.log('getOpenid fail 222', err);
            }
        });
    },
});
